(function() {
  var iced, jobs, moment, pgsql, sched, send_notify, take_term_effect, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  sched = require('node-schedule');

  pgsql = require('pg');

  moment = require('moment');

  uuid = require('uuid');

  jobs = {};

  module.exports = jobs;

  send_notify = function(message, detail, level) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 14
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          return done(client) && console.log(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee"
          });
          client.query("insert into web_notify ( sender, message, detail, level ) values ( $1, $2, $3, $4 )", [1, message, detail, level], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 23
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            return done(client) && console.log(err);
          }
          return done();
        });
      };
    })(this));
  };

  take_term_effect = function(term_uuid) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 30
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          return done(client) && console.log(err);
        }
        client.query("begin");
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee"
          });
          client.query("select * from pay_terms_ex where uuid = $1 for update", [term_uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 37
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            return done(client) && console.log(err);
          }
          if (result.rows.length === 0) {
            client.query("rollback;");
            if (err) {
              return done(client) && console.log(err);
            }
            done();
            return console.log('未找到待生效的终端记录');
          }
          record = result.rows[0];
          if (record.rejected) {
            client.query("rollback");
            done();
            return console.log('终端信息复核时被拒绝，不能生效');
          }
          if (!record.checked) {
            client.query("rollback");
            done();
            return console.log('终端信息尚未复核，不能生效');
          }
          switch (record.action) {
            case 1:
            case 2:
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee"
                });
                client.query("update pay_terms as t set termid = e.termid,           merid = e.merid, mapto_term = e.mapto_term, tmk = e.tmk,                 macalgo = e.macalgo, notes = e.notes, inputer = e.inputer,         input_time = e.input_time, checker = e.checker,         check_time = e.check_time, effect_time = e.effect_time, shaded = false from ( select termid,         merid, mapto_term, tmk,            macalgo, notes, inputer,        input_time, checker,        check_time,     effect_time, ref_uuid,       uuid from pay_terms_ex ) as e where t.uuid = e.ref_uuid and e.uuid = $1", [record.uuid], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 79
                }));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  if (err) {
                    client.query("rollback");
                    done();
                    console.log(err.message);
                    if (err.detail) {
                      console.log(err.detail);
                    }
                    return sched.scheduleJob(moment().add('1', 'm').toDate(), function() {
                      return take_term_effect(term_uuid);
                    });
                    return __iced_k();
                  } else {
                    (function(__iced_k) {
                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                        parent: ___iced_passed_deferral,
                        filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee"
                      });
                      client.query("delete from pay_terms_ex where uuid = $1", [term_uuid], __iced_deferrals.defer({
                        assign_fn: (function() {
                          return function() {
                            err = arguments[0];
                            return result = arguments[1];
                          };
                        })(),
                        lineno: 92
                      }));
                      __iced_deferrals._fulfill();
                    })(function() {
                      client.query("commit");
                      return done();
                      return __iced_k();
                    });
                  }
                })(__iced_k);
              });
              break;
            case 3:
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee"
                });
                client.query("delete from pay_terms_ex where uuid = $1", [term_uuid], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 100
                }));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee"
                  });
                  client.query("delete from pay_terms where uuid = $1", [record.ref_uuid], __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        err = arguments[0];
                        return result = arguments[1];
                      };
                    })(),
                    lineno: 105
                  }));
                  __iced_deferrals._fulfill();
                })(function() {
                  if (err) {
                    client.query("rollback");
                    done();
                    console.log(err.message);
                    console.log(err.detail);
                    sched.scheduleJob(moment().add('1', 'm').toDate(), function() {
                      return take_term_effect(term_uuid);
                    });
                  } else {
                    client.query("commit");
                    return done();
                  }
                  return __iced_k();
                });
              });
              break;
            default:
              return __iced_k(send_notify('系统错误', "复核表中 action 无效，UUID = " + term_uuid, 4));
          }
        });
      };
    })(this));
  };

  jobs.sched_effect = function(term_uuid, effect_time) {
    if (typeof effect_time === 'string') {
      effect_time = moment(effect_time);
    }
    if (effect_time.isBefore(moment().add('1', 'm'))) {
      return take_term_effect(term_uuid);
    } else {
      return sched.scheduleJob(effect_time.toDate(), function() {
        return take_term_effect(term_uuid);
      });
    }
  };

  jobs.add_all = function() {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee",
          funcname: "add_all"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 137
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          return done(client) && next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/terms_jobs.coffee",
            funcname: "add_all"
          });
          client.query("select * from pay_terms_ex where checked = true", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 142
          }));
          __iced_deferrals._fulfill();
        })(function() {
          var _i, _len, _ref, _results;
          if (err) {
            return done(client) && next(err);
          }
          done();
          _ref = result.rows;
          _results = [];
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            record = _ref[_i];
            _results.push(jobs.sched_effect(record.uuid, record.effect_time));
          }
          return _results;
        });
      };
    })(this));
  };

}).call(this);
